<template>
  <view>
    <nav-custom :showSearch="false"></nav-custom>
    <view class="login">
      <view class="login-title color-2c f16 bold margin-bottom-10">账号密码登录</view>
      <view class="login-input margin-bottom-20">
        <u-input placeholder="请输入账号" border="surround" shape="circle" v-model="userInfo.username" focus></u-input>
        <u-input placeholder="请输入密码" border="surround" shape="circle" v-model="userInfo.password" password></u-input>
      </view>
      <u-button text="立即登录" color="#ffe32a" shape="circle" @click="handleLogin"></u-button>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        userInfo: {
          username: '',
          password: ''
        },
      };
    },
    methods: {
      handleLogin() {
        const {
          username,
          password
        } = this.userInfo
        if (username && password) {
          this.$H.post(`/1.1/login`, {
            username,
            password
          }).then(res => {
            if (res.statusCode == 200) {
              this.$store.commit('user/saveUserInfo', res.data)
              uni.navigateBack({
                delta: 1,
                success: () => {
                  this.$store.commit('cart/changeLoginShow',false)
                  uni.showToast({
                    title: '登录成功',
                    icon: 'none'
                  })
                }
              })
            }
          }).catch((err) => {
            console.log(err);
            err.data.code == 211 && uni.showToast({
                    title: '账号或密码错误',
                    icon: 'none',
                  })
          })
        } else {
          uni.showToast({
            title: '请填写账号和密码',
            icon: 'none'
          })
        }
      }
    },
  }
</script>

<style lang="scss" scoped>
  /deep/ .u-button__text {
    color: #2c2c2c;
    font-size: 36rpx !important;
  }

  /deep/ .u-input {
    margin-bottom: 20rpx;
    background-color: #ededed;
  }

  .login {
    width: 90%;
    padding-top: 208rpx;
    margin: 0 auto;
  }
</style>
